package com.wzy.jsoupdemo;

import com.wzy.pojo.Article;
import com.wzy.util.ConnectionUtil;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.junit.Test;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述  :
 * 创建人:    Wzy
 * 创建时间: 2020-06-05  13:46
 */
public class JsoupTest {
    @Test
    public void test() throws IOException {
        String targetUrl = "https://zhipeng0908.gitee.io/";
        Connection connect = Jsoup.connect(targetUrl);
        connect.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        connect.header("Accept-Encoding", "gzip, deflate, br");
        connect.header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        connect.header("Connection", "keep-alive");
        connect.header("Host", "zhipeng0908.gitee.io");
        connect.header("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36");
        connect.ignoreHttpErrors(true);
        //执行
        Connection.Response response = connect.method(Connection.Method.GET).execute();
        //获取结果
        String body = response.body();
        System.out.println(body);
    }

    /**
     * 测试获取博客标题
     *
     * @throws IOException
     */
    @Test
    public void test2() throws IOException {
        String targetUrl = "https://zhipeng0908.gitee.io/";
        Connection connect = Jsoup.connect(targetUrl);
        connect.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        connect.header("Accept-Encoding", "gzip, deflate, br");
        connect.header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        connect.header("Connection", "keep-alive");
        connect.header("Host", "zhipeng0908.gitee.io");
        connect.header("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36");
        connect.ignoreHttpErrors(true);
        //执行
        Connection.Response response = connect.method(Connection.Method.GET).execute();
        //获取document, 就是爬取的整个页面
        Document document = response.parse();
        //获取 页面的 html body标签
        Element bodyElement = document.body();
        //  System.out.println(bodyElement);
        //获取卡片式图
        Elements cardElements = bodyElement.select(".post-block");
        for (Element cardElement : cardElements) {
            //  System.out.println(cardElement.html());
            Elements titleElement = cardElement.select(".post-block .post-title-link");
            /*text 方法用于获取元素节点内的文本内容
             * HTML方法用于获取元素节点内的html标签*/
            String title = titleElement.text();
            System.out.println("文章标题:" + title);
            Elements typeElement = cardElement.select(".post-category");
            String text = typeElement.text().replace("| 分类于", "");
            System.out.println("文章分类" + text);

            /*超链接的获取,获取标的属性 使用 attr*/
            String href = targetUrl + titleElement.attr("href");
            System.out.println("连接:" + href);
        }
    }

    /**
     * 发布时间 和 文章作者
     *
     * @throws IOException
     * @throws ParseException
     */
    @Test
    public void test3() throws IOException, ParseException {
        String targetUrl = "https://zhipeng0908.gitee.io//2020/03/26/前端/webpack03VueCli/";
        Connection connect = Jsoup.connect(targetUrl);
        connect.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        connect.header("Accept-Encoding", "gzip, deflate, br");
        connect.header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        connect.header("Connection", "keep-alive");
        connect.header("Host", "zhipeng0908.gitee.io");
        connect.header("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36");
        connect.ignoreHttpErrors(true);
        //执行
        Connection.Response response = connect.method(Connection.Method.GET).execute();
        //获取document, 就是爬取的整个页面
        Document document = response.parse();
        //获取 页面的 html body标签
        Element bodyElement = document.body();
        // System.out.println(bodyElement);
        Elements contentElement = bodyElement.select(".post-body");
        String content = contentElement.html();
        Elements authorElement = bodyElement.select(".my_post_copyright> p:nth-child(5)");
        String author = authorElement.text().replace("文章作者:", "");
        Elements dateElement = bodyElement.select(".my_post_copyright> p:nth-child(6)");
        String date = dateElement.text().replace("发布时间:", "");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 - HH:mm");
        Date createDate = simpleDateFormat.parse(date);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        date = dateFormat.format(createDate);

        System.out.println("文章作者:" + author);
        System.out.println("发布时间:" + date);
    }

    @Test
    public void test4() throws Exception {
        String targetUrl = "https://zhipeng0908.gitee.io/";
        /*自定义工具类*/
        Connection connect = ConnectionUtil.getConnection(targetUrl);
        //执行
        Connection.Response response = connect.method(Connection.Method.GET).execute();
        //获取document, 就是爬取的整个页面
        Document document = response.parse();
        //获取 页面的 html body标签
        Element bodyElement = document.body();
        //  System.out.println(bodyElement);
        //获取卡片式图
        Elements cardElements = bodyElement.select(".post-block");
        for (Element cardElement : cardElements) {
            //  System.out.println(cardElement.html());
            Elements titleElement = cardElement.select(".post-block .post-title-link");
            /*text 方法用于获取元素节点内的文本内容
             * HTML方法用于获取元素节点内的html标签*/
            String title = titleElement.text();
            System.out.println("文章标题:" + title);
            Elements typeElement = cardElement.select(".post-category");
            String text = typeElement.text().replace("| 分类于", "");
            System.out.println("文章分类:" + text);

            /*超链接的获取,获取标的属性 使用 attr*/
            String href = targetUrl + titleElement.attr("href");
            System.out.println("连接:" + href);

            /*------------------------------------------------------*/

            Connection childConnect = ConnectionUtil.getConnection(href);
            //执行
            Connection.Response childResponse = childConnect.method(Connection.Method.GET).execute();
            //获取document, 就是爬取的整个页面
            Document childDocument = childResponse.parse();
            //获取 页面的 html body标签
            Element childDodyElement = childDocument.body();
            // System.out.println(bodyElement);
            Elements contentElement = childDodyElement.select(".post-body");
            String content = contentElement.html();
            Elements authorElement = childDodyElement.select(".my_post_copyright> p:nth-child(5)");
            String author = authorElement.text().replace("文章作者:", "");
            Elements dateElement = childDodyElement.select(".my_post_copyright> p:nth-child(6)");
            String date = dateElement.text().replace("发布时间:", "");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 - HH:mm");
            Date createDate = simpleDateFormat.parse(date);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date = dateFormat.format(createDate);

            System.out.println("文章作者:" + author);
            System.out.println("发布时间:" + date);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        }
    }

    @Test
    public void test0() throws Exception {
        String childUrl = "https://zhipeng0908.gitee.io//2020/02/28/其他/博客说明/";
        Connection childConnect = Jsoup.connect(childUrl);
        childConnect.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
        childConnect.header("Accept-Encoding", "gzip, deflate, br");
        childConnect.header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        childConnect.header("Connection", "keep-alive");
        childConnect.header("Host", "zhipeng0908.gitee.io");
        childConnect.header("User-Agent", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Mobile Safari/537.36");
        childConnect.ignoreHttpErrors(true);
        //执行
        Connection.Response childResponse = childConnect.method(Connection.Method.GET).execute();
        //获取document, 就是爬取的整个页面
        Document childDocument = childResponse.parse();
        //获取 页面的 html body标签
        Element childDodyElement = childDocument.body();
        // System.out.println(bodyElement);
        Elements contentElement = childDodyElement.select(".post-body");
        String content = contentElement.html();
        Elements authorElement = childDodyElement.select(".my_post_copyright> p:nth-child(5)");
        String author = authorElement.text().replace("文章作者:", "");
        Elements dateElement = childDodyElement.select(".my_post_copyright> p:nth-child(6)");
        String date = dateElement.text().replace("发布时间:", "");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 - HH:mm");
        Date createDate = simpleDateFormat.parse(date);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        date = dateFormat.format(createDate);

        System.out.println("文章作者:" + author);
        System.out.println("发布时间:" + date);
    }

    /**
     * 测试单个对象
     *
     * @throws Exception
     */
    @Test
    public void testFramework() throws Exception {
        Article article = (Article) ConnectionUtil.execute(Article.class, "", new Article());
        System.out.println(article);
    }

    /**
     * 测试集合对象
     *
     * @throws Exception
     */
    @Test
    public void testFrameworkList() throws Exception {
        List<Article> execute = (List<Article>) ConnectionUtil.execute(Article.class, "", new ArrayList<Article>());
        for (Article article : execute) {
            System.out.println(article);
        }
    }
}
